package com.example.msg.controller;

import com.example.msg.service.MsgService;
import com.example.msg.utils.RandomUtil;
import com.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/msg")
@CrossOrigin
public class MsgController {

    @Autowired
    MsgService msgService;

    @Autowired
    RedisTemplate<String ,String> redisTemplate;

    @GetMapping("/sendMsg/{phoneNumber}")
    public Result sendMsg(@PathVariable String phoneNumber){

        String code = RandomUtil.getFourBitRandom();
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        System.out.println(code);
        redisTemplate.opsForValue().set(phoneNumber,code);
        System.out.println(redisTemplate.opsForValue().get(phoneNumber));
        msgService.send(param, phoneNumber);

        return Result.ok();
    }
    
    @GetMapping("/send")
    public Result send(){

       System.out.println("hahaha");
       return Result.ok();
    }
}
